TCS Ninja Coding Questions and Answers
| Time | 30 mins |
| Questions | 1 |
| Negative Marking | No |
| Languages | C, C++, Java, Perl, Python |
TCS Ninja Coding Questions and Answers 2020
TCS Ninja coding questions answers 2020 is available here.Ninja coding questions paper with solutions is very important for TCS ninja exam. Also, you will find TCS ninja coding questions with solved papers. On PrepInsta you will find the Accurate TCS Ninja coding exam Pattern 2021.
- No Of Questions:- 1 Question
- Time – 30 mins
- There will be no negative marking.
- TCS NQT is adaptive this year
- You will not get any extra rough paper in the exam as a calculator and Rough Paper will be available on your Desktop Screen. You are not allowed to move your eyes down while giving the examination.
Paid Material
According to the latest syllabus of TCS
- TCS Coding Materials – 1
- TCS Coding Materials – 2
- TCS Coding Materials – 3
- TCS Coding Materials – 4
- TCS Coding Materials – 5
- TCS Coding Materials – 6
- TCS Coding Materials – 7
- TCS Coding Materials – 8
- TCS Coding Materials – 9
- TCS Coding Materials – 10
- TCS Coding Materials – 11
- TCS Coding Materials – 12
- TCS Coding Materials – 13
- TCS Coding Materials – 14
- TCS Coding Materials – 15
- TCS Coding Materials – 16
- TCS Coding Materials – 17
- TCS Coding Materials – 18
- TCS Coding Materials – 19
- TCS Coding Materials – 20
- TCS Coding Materials – 21
- TCS Coding Materials – 22
- TCS Coding Materials – 23
- TCS Coding Materials – 24
- TCS Coding Materials – 25
- TCS Coding Materials – 26
- TCS Coding Materials – 27
- TCS Coding Materials – 28
- TCS Coding Materials – 29
- TCS Coding Materials – 30
- TCS Coding Materials – 31
- TCS Coding Materials – 32
- TCS Coding Materials – 33
- TCS Coding Materials – 34
- TCS Coding Materials – 35
- TCS Coding Materials – 36
- TCS Coding Materials – 37
- TCS Coding Materials – 38
- TCS Coding Materials – 39
- TCS Coding Materials – 40
- TCS Coding Materials – 41
- TCS Coding Materials – 42
- TCS Coding Materials – 43
- TCS Coding Materials – 44
- TCS Coding Materials – 45
- TCS Coding Materials – 46
- TCS Coding Materials – 47
- TCS Coding Materials – 48
- TCS Coding Materials – 49
- TCS Coding Materials – 50
- TCS Coding Materials – 51
- TCS Coding Materials – 52
- TCS Coding Materials – 53
- TCS Coding Materials – 54
- TCS Coding Materials – 55
- TCS Coding Materials – 56
- TCS Coding Materials – 57
- TCS Coding Materials – 58
- TCS Coding Materials -59
Based on Previous Year Patterns
- TCS Coding Question -1
- TCS Coding Questions – 2
- TCS Coding Questions – 3
- TCS Coding Questions – 4
- TCS Coding Questions – 5
- TCS Coding Questions – 6
- TCS Coding Questions – 7
- TCS Coding Questions – 8
- TCS Coding Questions – 9
- TCS Coding Questions – 10
- TCS Coding Questions – 11
- TCS Coding Questions – 12
- TCS Coding Questions – 13
- TCS Coding Questions – 14
- TCS Coding Questions – 15
- TCS Coding Questions – 16
- TCS Coding Questions – 17
- TCS Coding Questions – 18
- TCS Coding Questions – 19
- TCS Coding Questions – 21
- TCS Coding Questions – 22
- TCS Coding Questions – 23
- TCS Coding Questions – 24
- TCS Coding Questions – 25
- TCS Coding Questions – 26
- TCS Coding Questions – 27
- TCS Coding Questions – 28
Word is the Key 💖 (Link to this question)
(TCS Ninja – Aug 2019 Slot 1)
One programming language has the following keywords that cannot be used as identifiers:
break, case, continue, default, defer, else, for, func, goto, if, map, range, return, struct, type, var
Write a program to find if the given word is a keyword or not
Test cases
Case 1
- Input – defer
- Expected Output – defer is a keyword
Case 2
- Input – While
- Expected Output – while is not a keyword
#include<stdio.h> #include<string.h> int main(){ char str[16][10] = {"break", "case", "continue", "default", "defer", "else","for", "func", "goto", "if", "map", "range", "return", "struct", "type", "var"}; char input[20]; int flag = 0; scanf("%s",input); for(int i = 0; i<16;i++){ if(strcmp(input,str[i]) == 0){ flag = 1; break; } } if(flag==1){ printf("%s is a keyword",input); } else{ printf("%s is not a keyword",input); } return 0; }
Program 2
Sweet Seventeen
Sweet Seventeen 💖 (Link to this question)
(TCS Ninja – Aug 2019 Slot 2)
Given a maximum of four digit to the base 17 (10 – A, 11 – B, 12 – C, 13 – D … 16 – G} as input, output its decimal value.
Test Cases
Case 1
- Input – 1A
- Expected Output – 27
Case 2
- Input – 23GF
- Expected Output – 10980
#include <stdio.h> #include <math.h> #include <string.h> int main(){ char hex[17]; long long decimal, place; int i = 0, val, len; decimal = 0; place = 1; scanf("%s",hex); len = strlen(hex); len--; for(i = 0;hex[i]!='\0';i++) { if(hex[i]>='0'&& hex[i]<='9'){ //48 to 57 are ascii values of 0 - 9 //say value is 8 its ascii will be 56 //val = hex[i] - 48 => 56 - 48 => val = 8 val = hex[i] - 48; } else if(hex[i]>='a'&& hex[i]<='g'){ //97 to 103 are ascii values of a - g //say value is g its ascii will be 103 //val = hex[i] - 97 + 10 => 103 - 97 + 10=> val = 16 //10 is added as g value is 16 not 6 or a value is 10 not 0 val = hex[i] - 97 + 10; } else if(hex[i]>='A'&& hex[i]<='G'){ //similarly, 65 to 71 are values of A - G val = hex[i] - 65 + 10; } decimal = decimal + val * pow(17,len); len--; } printf("%lld",decimal); return 0; }
Program 3
Oddly Even
The Oddly even 💖 (Link to this question)
(TCS Ninja – Aug 2019 Slot 3)
Problem
Given a maximum of 100 digit numbers as input, find the difference between the sum of odd and even position digits
Test Cases
Case 1
- Input: 4567
- Expected Output: 2
Explanation : Odd positions are 4 and 6 as they are pos: 1 and pos: 3, both have sum 10. Similarly, 5 and 7 are at even positions pos: 2 and pos: 4 with sum 12. Thus, difference is 12 – 10 = 2
Case 2
- Input: 5476
- Expected Output: 2
Case 3
- Input: 9834698765123
- Expected Output: 1
#include <stdio.h> #include <string.h> #include <stdlib.h> int main() { int a = 0,b = 0,i = 0, n; char num[100]; printf("Enter the number:"); scanf("%s",num); //get the input up to 100 digit n = strlen(num); while(n>0) { if(i==0) //add even digits when no of digit is even and vise versa { a+=num[n-1]-48; n--; i=1; } else //add odd digits when no of digit is even and vice versa { b+=num[n-1]-48; n--; i=0; } } printf("%d",abs(a-b)); //print the difference of odd and even return 0; }
Program 4
Position prediction
The Sholka Problem 💖 (Link to this question)
(TCS Ninja – Aug 2019 Slot 4)
Our hoary culture had several great persons since time immemorial and king vikramaditya’s nava ratnas (nine gems) belongs to this ilk. They are named in the following shloka:


Among these, Varahamihira was an astrologer of eminence and his book Brihat Jataak is recokened as the ultimate authority in astrology.
He was once talking with Amarasimha,another gem among the nava ratnas and the author of Sanskrit thesaurus, Amarakosha.
Amarasimha wanted to know the final position of a person, who starts from the origin 0 0 and travels per following scheme.
Scheme
- He first turns and travels 10 units of distance
- His second turn is upward for 20 units
- Third turn is to the left for 30 units
- Fourth turn is the downward for 40 units
- Fifth turn is to the right(again) for 50 units
… And thus he travels, every time increasing the travel distance by 10 units.
Test Cases
Case 1
- Input : 3
- Expected Output :-20 20
Case 2
- Input: 4
- Expected Output: -20 -20
Case 3
- Input : 5
- Expected Output : 30 -20
Case 4
- Input : 7
- Expected Output : 90 -20


#include <stdio.h> #include <stdlib.h> int main() { int n; scanf("%d", &n); char c = 'R'; int x = 0, y = 0; int distance = 10; while(n) { switch(c) { case 'R': x = x + distance; c = 'U'; distance = distance + 10; break; case 'U': y = y + distance; c = 'L'; distance = distance + 10; break; case 'L': x = x - distance; c = 'D'; distance = distance + 10; break; case 'D': y = y - distance; c = 'A'; distance = distance + 10; break; case 'A': x = x + distance; c = 'R'; distance = distance + 10; break; } n--; } printf("%d %d",x,y); return 0; }
Program 5
Leap Year or not
Program to check if a year is Leap Year or not 💖
(TCS Ninja – June 2019 Slot 1)
#include<stdio.h>
int main()
{
int year;
printf("Enter a year to check if it is a leap year\n");
scanf("%d", &year);
if (year%400 == 0) // Exactly divisible by 400 e.g. 1600, 2000
printf("%d is a leap year.\n", year);
else if (year%100 == 0) // Exactly divisible by 100 and not by 400 e.g. 1900, 2100
printf("%d isn't a leap year.\n", year);
else if (year%4 == 0) // Exactly divisible by 4 and neither by 100 nor 400 e.g. 2016, 2020
printf("%d is a leap year.\n", year);
else // Not divisible by 4 or 100 or 400 e.g. 2017, 2018, 2019
printf("%d isn't a leap year.\n", year);
return 0
}
Program 6
Prime Number with a Twist
Ques. Write a code to check whether no is prime or not. Condition use function check() to find whether entered no is positive or negative ,if negative then enter the no, And if yes pas no as a parameter to prime() and check whether no is prime or not?
(TCS Ninja – June 2019 Slot 2)
#include<bits/stdc++.h>
using namespace std;
void prime(int num){
int count=0;
for(int i=2;i<num;i++){
if(num%i==0){
count++;
break;
}
}
if(count==0){
cout<<"prime"<<endl;
}
else{
cout<<"Not Prime"<<endl;
}
}
int main(){
int n;
cout<<"Enter the number: ";
cin>>n;
if(n>0){
prime(n);
}
else{
cout<<"negative number.Please enter a postive number"<<endl;
}
return 0;
}
Program 7
Number Series Type 1
Ques. Find the 15th term of the series?
0,0,7,6,14,12,21,18, 28
Please add the answer in the comment section below.
(TCS Ninja – Dec 2018 Slot 1)
Program 8
Number Series Type 2
Problem
Question. Find the nth term of the series.
1, 1, 2, 3, 4, 9, 8, 27, 16, 81, 32, 243,64, 729, 128, 2187 ….
This series is a mixture of 2 series – all the odd terms in this series form a geometric series and all the even terms form yet another geometric series. Write a program to find the Nth term in the series.
- The value N in a positive integer that should be read from STDIN.
- The Nth term that is calculated by the program should be written to STDOUT.
- Other than value of n th term,no other character / string or message should be written to STDOUT.
- For example , if N=16, the 16th term in the series is 2187, so only value 2187 should be printed to STDOUT.
You can assume that N will not exceed 30.
Test Case 1
- Input- 16
- Expected Output – 2187
Test Case 2
- Input- 13
- Expected Output – 64
(TCS Ninja – Dec 2018 Slot 2)
Explanation
1, 1, 2, 3, 4, 9, 8, 27, 16, 81, 32, 243,64, 729, 128, 2187 can represented as :
- 2(0), 3(0),2(1), 3(1),2(2), 3(2),2(3), 3(3),2(4), 3(4),2(5), 3(5),2(6), 3(6) ….
There are two consecutive sub GP’s at even and odd positions
- (GP-1) At Odd Positions (Powers of 2) – 1, 2, 4, 8, 16, 32, 64, 128
- (GP-2) At Even Positions (Powers of 3) – 1, 3, 9, 27, 81, 243, 729, 2187
Clearly, for calculating Nth position value
- If N is Even, Find (N/2) position in sub GP – 2
- If N is Odd, Find (N/2 + 1) position in sub GP – 1
#include<stdio.h> #include<math.h> int three(int n) { int x; //n-1 because powers start from 0 not 1 x = pow(3,n-1); printf("%d",x); } int two(int n) { int x; //n-1 because powers start from 0 not 1 x = pow(2,n-1); printf("%d",x); } int main() { int n; scanf("%d",&n); //Checking of the nth term will be at even position or odd position //Odd positions are powers of 2 //Even positions are powers of 3 if(n%2==0) { //nth position(if even) will be at n/2 position for sub GP-2 three(n/2); } else { //nth position(if odd) will be at (n/2 + 1) position for sub GP-1 two(n/2 + 1); } return 0; }
Program 9
String based Operation
Question
1. The program will recieve 3 English words inputs from STDIN
- These three words will be read one at a time, in three separate line
- The first word should be changed like all vowels should be replaced by *
- The second word should be changed like all consonants should be replaced by @
- The third word should be changed like all char should be converted to upper case
- Then concatenate the three words and print them
Other than these concatenated word, no other characters/string should or message should be written to STDOUT
For example if you print how are you then output should be h*wa@eYOU.
You can assume that input of each word will not exceed more than 5 chars
Test Cases
Case 1
Input
- how
- are
- you
Expected Output : h*wa@eYOU
Case 2
Input
- how
- 999
- you
Expected Output : h*w999YOU
(TCS Ninja – Sep 3 2018 slot 1)
#include<stdio.h> #include<conio.h> #include<string.h> int main() { int i; char a[100],b[100],c[100]; scanf("%s",a); scanf("%s",b); scanf("%s",c); for(i=0;a[i]!='\0';i++) { if(a[i]=='a'||a[i]=='e'||a[i]=='i'||a[i]=='o'||a[i]=='u'||a[i]=='A'||a[i]=='E'||a[i]=='I'||a[i]=='O'||a[i]=='U') a[i]='*'; } for(i=0;b[i]!='\0';i++) {
if((b[i]>='a'&&b[i]<='z') || (b[i]>='A'&&b[i]<='Z')) if(!(b[i]=='a'||b[i]=='e'||b[i]=='i'||b[i]=='o'||b[i]=='u'||b[i]=='A'||b[i]=='E'||b[i]=='I'||b[i]=='O'||b[i]=='U')) b[i]='@'; } for(i=0;c[i]!='\0';i++) { if(c[i]>='a'&&c[i]<='z') c[i]=c[i]-32; } printf("%s%s%s",a,b,c); return 0; }
Program 10
Number Series based Iteration
Problem
Consider the below series :
0, 0, 2, 1, 4, 2, 6, 3, 8, 4, 10, 5, 12, 6, 14, 7, 16, 8
Program 11
Number Series based Operation
Question 11
Consider the below series:
1, 2, 1, 3, 2, 5, 3, 7, 5, 11, 8, 13, 13, 17…..
This series is a mixture of 2 series fail the odd terms in this series form a Fibonacci series and all the even terms are the prime numbers in ascending order
Write a program to find the Nth term in this series
The value N in a positive integer that should be read from mm. The Nth term that is calculated by the program should be written to STDOUT Otherthan the value of Nth term , no other characters / string or message should be written to STDOUT.
For example, when N:14, the 14th term in the series is 17 So only the value 17 should be printed to STDOUT
#include<stdio.h> void fibo(int); void prime(int); main() { int n,e; scanf("%d",&n); e=n/2; if(n%2==0) prime(e); else fibo(e+1); } void prime(int n) { int i,j,no,flag=0,count=0; for(i=1;i<=100;i++) { flag=0; for(j=2;j<=i/2;j++) { if(i%j==0) flag=0; else flag=1; } if(flag==1) count++; if(count==n) { printf("%d\n",i); break; } } } void fibo(int n) { int n0=0,n1=1,n2,i; for(i=3;i<=n;i++) { n2=n0+n1; n0=n1; n1=n2; } printf("%d",n2); }
Other Important Programs
Misc
Question
1. Using a method, pass two variables and find the sum of two numbers.
Test case:
Number 1 – 20
Number 2 – 20.38
Sum = 40.38
There were a total of 4 test cases. Once you compile 3 of them will be shown to you and 1 will be a hidden one. You have to display error message if numbers are not numeric.
(TCS Ninja – June 2019 Slot 3)
Command Line Programs (Not asked anymore)
Command Line
Question 1:
Finding GCD of two numbers
// C program to calculate GCD of two numbers #include <stdio.h> // The code used a recursive function to return gcd of p and q int gcd(int p, int q) { // checking divisibility by 0 if (p == 0) return q;
if (q == 0) return p; // base case if (p == q) return p;
// p is greater if (p > q) return gcd(p-q, q); else return gcd(p, q-p); } // Driver program to test above function int main() {
int p = 98, q = 56; printf("GCD of %d and %d is %d ", p, q, gcd(p, q)); return 0; }
Question 2
Binary to Decimal Conversion
/** C program to convert the given binary number into decimal**/ #include<stdio.h> int main() { int num, binary_val, decimal_val = 0, base = 1, rem; printf("Insert a binary num (1s and 0s) \n"); scanf("%d", &num); /* maximum five digits */ binary_val = num; while (num > 0) { rem = num % 10; decimal_val = decimal_val + rem * base; //num/=10; num = num / 10 ; //base*=2; base = base * 2; } //display binary number printf("The Binary num is = %d \n", binary_val); //display decimal number printf("Its decimal equivalent is = %d \n", decimal_val); return 0; }
Question 3
Armstrong Number
#include<stdio.h> int main() { int num ,n,n1,c=0,mul=1,sum=0,r,f,i; printf("enter any num: \n"); scanf("%d",&num); n=num; n1=num; while(n!=0) { r=n%10; c++; n=n/10; } while (num!=0) { f=num%10; mul=1; for(i=1;i<=c;i++) { mul=mul*f; } sum=sum+mul; num=num/10; } if(n1==sum) printf("Armstrong Number"); else printf("Not an Armstrong Number"); return 0; }
Question 4
Write a C program to find the area of a circle with radius provided.
The value of radius positive integer passed to the program as the first command line parameter. Write the output to stdout formatted as a floating point number rounded to EXACTLY 2 decimal precision WITHOUT any other additional text.
Scientific format(such as 1.00E+5) should NOT be used while printing the output.
You may assume that the inputs will be such that the output will not exceed the largest possible real number that can be stored in a float type variable.
It is highly advisable to go through Command Line Arguments Post before even looking at the code. Please study this for TCS and come back to this post later.
#include<stdio.h> int main() { //for initialization of radius and area in a float datatype float radius,area,pi=3.14; // for use user input printf("Enter the Radius of a Circle : "); scanf("%f",&radius); //formula of area of circle area = pi*radius*radius; printf("Area of Circle is: %f",area); return 0; }
Question 5
Command Line Program to check if a year is Leap Year or Not
#include<stdio.h> int main() { //initialization of Year int year; //to take user input printf("Enter Year for find leap year or not : "); scanf("%d",&year); //we use this statement for check leap year if(((year%4==0)&&(year%100!=0)) || (year%400==0)) printf("%d is a Leap Year",year); //not leap year else printf("%d is not a Leap Year",year); return 0; }
Question 6
Fibonacci Series
#include<stdio.h> int main() { //To initialize variables int n1=0,n2=1,n3,limit,i; //To take user input printf("enter a limit of series \n"); scanf( "%d",&limit); printf("Fibonacci series %d %d ",n1,n2); //To use this loop for given length for(i=2;i<limit;i++) { //n1 and n2 sum store in new variable n3 n3=n1+n2; n1=n2; n2=n3; //display serious printf("%d ",n3); } return 0; }
Question 7
Ques. Write a C program to find the area of a triangle given the base and the corresponding height. The values base and height are both positive integers passed to the program as the first and second command line parameters respectively. Write the output to stdout formatted as a floating point number rounded to EXACTLY 2 decimal precision WITHOUT any other additional text. Scientific format(such as 1.00E+5) should NOT be used while printing the output. You may assume that the inputs will be such that the output will not exceed the largest possible real number that can be stored in a float type variable.
#include <stdio.h> #include <math.h> int calcArea(int a, int b, int c); void main() { int a, b, c; double area; printf("Enter the values of a, b and c \n"); scanf("%d %d %d", &a, &b, &c); area=calcArea(a,b,c); printf("Area of a triangle = %f \n", area); } int calcArea(int a, int b, int c) { double area=0.0, s=0.0; s = (a + b + c) / 2; area = sqrt(s * (s - a) * (s - b) * (s - c)); return area; }
Question 8
Palindrome Number
#include<stdio.h>
int main()
{
//Initialization of variables where rev='reverse=0'
int number, rev = 0,store, n1,left;
//input a numbers for user
printf("Enter the number\n");
scanf("%d", &number);
//for duplicacy of number
n1=number;
store= number;
//use this loop for check true condition
while (number > 0)
{
//left is for remider are left
left= number%10;
//for reverse of no.
rev = rev * 10 + left;
//number /= 10;
number=number/10;
}
//To check reverse no is a Palindrome
if(n1==rev)
printf("Number %d is Palindrome number",n1);
else
printf("it is not a Palindrome number");
return 0;
}
Question 9
Decimal to Binary
#include<stdio.h> int main() { //for initialize a variables long number, dec_num, rem, base = 1, bin = 0, count = 0; //To insert a number printf("Insert a decimal num \n"); scanf("%ld", &number); dec_num = number; while(number > 0) { rem = number % 2; /* To count no.of 1s */ if (rem == 1) { count++; } bin = bin + rem * base; //number/=2; number = number / 2; base = base * 10; } //display printf("Input num is = %d\n", dec_num); printf("Its binary equivalent is = %ld\n", bin); printf("Num of 1's in the binary num is = %d\n", count); return 0; }
Question 10
Binary to Octal
#include<stdio.h> int main() { //For initialize variables long int binary_num, octal_num = 0, j = 1, rem; //Inserting the binary number from the user printf("Enter a binary number: "); scanf("%ld", &binary_num); // while loop for number conversion while(binary_num != 0) { rem = binary_num % 10; octal_num = octal_num + rem * j; j = j * 2; binary_num = binary_num / 10; } printf("Equivalent octal value: %ld", octal_num); return 0; }
Question 11
Decimal to Octal
#include<stdio.h> int main() { //Variable initialization long dec_num, rem, quotient; int i, j, octalno[100]; //Taking input from user printf("Enter a number for conversion: "); //Storing the value in dec_num variable scanf("%ld",&dec_num); quotient = dec_num; i=1; //Storing the octal value in octalno[] array while (quotient!=0) { octalno[i++]=quotient%8; quotient=quotient/8; } //Printing the octalno [] in reverse order printf("\nThe Octal of %ld is:\n\n",dec_num); for (j=i-1;j>0;j--) //display it printf ("%d", octalno[j]); return 0; }
Question 12
String Palindrome
#include <stdio.h> #include <string.h> int main() { //Initializing variable. char str[100]; int i,length=0,flag=0; //Accepting input. printf("Enter the string : "); gets(str); length=strlen(str); //Initializing for loop. for(i=0;i<length/2;i++) { //Checking if string is palindrome or not. if(str[i]==str[length-i-1]) flag++; } //Printing result. if(flag==i) printf("String entered is palindrome"); else printf("String entered is not palindrome"); return 0; }
Question 13
Reverse of a number
#include<stdio.h> int main() { //Initialization of variables where rev='reverse=0' int number, rev = 0,store, left; //input a numbers for user printf("Enter the number\n"); scanf("%d", &number); store= number; //use this loop for check true condition while (number > 0) { //left is for remider are left left= number%10; //for reverse of no. rev = rev * 10 + left; //number /= 10; number=number/10; } //To show the user value printf("Given number = %d\n",store); //after reverse show numbers printf("Its reverse is = %d\n", rev); return 0; }
Question 14
Square Root without using Math.h
#include <stdio.h> double SqrtNumber(double num) { double min=0; double max=num; double temp=0; int nCount = 50; while(nCount != 0) { temp=(min+max)/2; if(temp*temp==num) { return temp; } else if(temp*temp > num) { max = temp; } else { min = temp; } nCount--; } return temp; } int main() { double num; printf("Enter the number\n"); scanf("%lf",&num); if(num < 0) { printf("Error: Negative number!"); return 0; } printf("Square roots is: %f",SqrtNumber(num)); return 0; }
Question 15
Average of two numbers
#include<stdio.h> int main() { int n1,n2,avg; printf("Enter first no:"); scanf("%d",&n1); printf("Enter first no:"); scanf("%d",&n2); avg=(n1+n2)/2; printf("Average is %d",avg); }
Question 16
Greatest of two Numbers
#include<stdio.h> int main() { int no1, no2; printf("Insert two numbers:"); scanf("%d %d",&no1, &no2); //Condition to check which of the two number is greater //it will compare of number where number 1 is greater if(no1 > no2) printf("%d is greatest",no1); //where number 2 is greater else if(no2 > no1) printf("%d is greatest",no2); //for both are equal else printf("%d and %d are equal", no1, no2); return 0; }
Question 17
Write a Program to print whether the given alphabet is vowel or consonant :
#include <stdio.h> int main () { char ch;
// Get the character char ch; scanf ("%c", &ch); if (ch >= 'A' && ch <= 'Z') { ch = 'a' + (ch - 'A'); } if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { printf ("Vowel"); } else { printf ("Consonant"); } return 0; }
Question 18
Write a program to Check whether a given number is a prime number or not :
#include <stdio.h> int main () { int n, i, flag = 0; printf ("Enter a positive integer: "); scanf ("%d", &n); for (i = 2; i <= sqrt (n); ++i) { // condition for non prime number if (n % i == 0) { flag = 1; break; } } if (flag == 0) printf ("%d is a prime number", n); else printf ("%d is not a prime number", n); return 0; }
Question 19
Write a program to change the case of the given alphabet and print:
#include <stdio.h> int main () { char c; scanf ("%c", &c); // Upper to lower case if ('A' <= c && c <= 'Z') { printf ("%c", 'a' + (c - 'A')); } // Lower to upper case if('a' <= c && c <= 'z') { printf ("%c", 'A' + (c - 'a')); } return 0; }
Question 20
Given an array and a number (say s), find whether any two elements in the array whose sum is “s”:
#include<stdio.h> #include<stdlib.h> void check_sum_and_diplay (int arr[], int size, int sum); int main () { // Get the size of an array int size; scanf ("%d", &size); // Get the array elements int arr[50], i; for (i = 0; i < size; i++) { scanf ("%d", &arr[i]); // Get the sum value (to check with an array elements) int sum; scanf ("%d", &sum); // Function call to check the sum of any two elements in an array equal to given sum // and display the same check_sum_and_diplay (arr, size, sum); return 0; } } void check_sum_and_diplay (int arr[], int size, int sum) { int i, j; for (i = 0; i < size - 1; i++) { if (sum == (arr[i] + arr[j])) { printf ("Perfect couple: %d %d", arr[i], arr[j]); exit (0); } printf ("No perfect couple found!"); } }
Question 21
Write a program to find the most occurring character in the string.
#include<stdio.h> #define MAX_SIZE 100 #define MAX_CHARS 26 int main () { int i; //Get a sentence char str[MAX_SIZE]; scanf ("%d", str); // Init Freq starting array int freq[MAX_CHARS]; for (i = 0; i < max_chars; i++) { freq[i] = 0; } //frequency of each character is counted for (i = 0; str[i] !='\0'; i++) { int isalphabet = 0, offset; if (str[i] >= 'a' && str[i] <= 'z') { isalphabet = 1; offset = str[i] - 'a'; } else if (str[i] >= 'A' && str[i] <= 'Z') { isAlphabet = 1; offset = str[i] - 'A'; } if (isAlphabet == 1) { freq[offset] += 1; } } // If two characters occurred the same number of time then // print lowest ASCII value character. int max_index = 0; for(i=0; i<max_chars;i++) { if (freq[i] > freq[max_index]) { max_index = i; } } int max_repeated_char = 'a' + max_index; printf ("%c", max_repeated_char); return 0; }










Qs 11 may be solved in python as….
def prime(n):
i,j=0,2
while i2: return(fibo(n-1)+fibo(n-2))
n=int(input(‘Enter N:’))
if n%2==0: print(prime(n//2))
else: print(fibo(n//2+1))
I think Programming 11 has some issue, plz chek it
ok Syed we’ll see to it
Please share the Code of WaterMark Solution Problem of TCS phase 2 test slot 1 on 1st sep 2019.
Will any command line programs come in TCS NQT 2020 in last round of coding section
Hi,
Command Line will not be used as a coding language in TCS Ninja Test. However, you need to learn command line programming, it is very important that you do. As 1 MCQ question in programming Logic section will be asked from command line programming basics.
Thank you sir. The questions that you gave above was repeated in my Bsc for tcs that happened on July 5, 2019. Thank you so much.
It was exact same I even knew the code as I did it a day before from your website. I hope that I clear my interview as well
what is the cut-off marks for TCS Ninja and how much we did need to score in order to get selected for TCS Digital .
Thank you sir. I got similar questions in the exam as well. Not the same question but the pattern was same.
Tcs paper pattern has changed.
They have kept args prog in mcq and coding section has changed.
Where can i get coding qstns according to the new one?